﻿using CSRedis;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using VOL.Core.DBManager;

namespace VOL.WebApi.Controllers.Finance.Partial
{
    [Route("api/yjy_whole_data_comparison")]
    [ApiController]
    public class yjy_whole_data_comparison:Controller
    {
        //根据查询条件获取整车物料号
        private List<dynamic> GetMaterialNum(string vehicle_model,string announcement_model,string fuel_type,string pilothouse,string cab_suspension,string drive,string wheelbase,string frame)
        {
            List<dynamic> result=new List<dynamic>();
            try
            {
                string serial_numbers = "";
                string describes = "";
                int count = 0;
                if (!string.IsNullOrEmpty(vehicle_model) && !vehicle_model.Equals("null"))
                {
                    serial_numbers += "'1',";
                    describes += "'" + vehicle_model + "',";
                    count += 1;
                }
                if (!string.IsNullOrEmpty(announcement_model) && !announcement_model.Equals("null"))
                {
                    serial_numbers += "'2',";
                    describes += "'" + announcement_model + "',";
                    count += 1;
                }
                if (!string.IsNullOrEmpty(fuel_type) && !fuel_type.Equals("null"))
                {
                    serial_numbers += "'3',";
                    describes += "'" + fuel_type + "',";
                    count += 1;
                }
                if (!string.IsNullOrEmpty(pilothouse) && !pilothouse.Equals("null"))
                {
                    serial_numbers += "'4',";
                    describes += "'" + pilothouse + "',";
                    count += 1;
                }
                if (!string.IsNullOrEmpty(cab_suspension) && !cab_suspension.Equals("null"))
                {
                    serial_numbers += "'5',";
                    describes += "'" + cab_suspension + "',";
                    count += 1;
                }
                if (!string.IsNullOrEmpty(drive) && !drive.Equals("null"))
                {
                    serial_numbers += "'6',";
                    describes += "'" + drive + "',";
                    count += 1;
                }
                if (!string.IsNullOrEmpty(wheelbase) && !wheelbase.Equals("null"))
                {
                    serial_numbers += "'7',";
                    describes += "'" + wheelbase + "',";
                    count += 1;
                }
                if (!string.IsNullOrEmpty(frame) && !frame.Equals("null"))
                {
                    serial_numbers += "'8',";
                    describes += "'" + frame + "',";
                    count += 1;
                }
                serial_numbers = serial_numbers.TrimEnd(',');
                describes = describes.TrimEnd(',');
                string sql = "select materialnum,materialnum_name from yjy_whole_base_serial_info where serial_number in (" + serial_numbers +")and [describe] in ("+ describes +") group by materialnum,materialnum_name having(count(*))="+ count;
                result = DBServerProvider.SqlDapper.QueryList<dynamic>(sql, null).ToList();
            }catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return result;
        }
        //根据整车物料号确定树形结构未展开时，需要展示的数据
        private List<dynamic> GetTableRow(string materialnum)
        {
            List<dynamic> result = new List<dynamic>();
            try
            {
                string sql = "select ywbsi.hierarchy_describe,ywbsi.[describe],ywbsi.componentnum,count(yww.parent_componentnum) as count from yjy_whole_base_serial_info ywbsi left join yjy_whole_wlzb yww on ywbsi.componentnum =yww.parent_componentnum  where ywbsi.materialnum='"+materialnum+"' group by ywbsi.hierarchy_describe,serial_number,ywbsi.[describe],ywbsi.componentnum  order by ywbsi.serial_number";
                result= DBServerProvider.SqlDapper.QueryList<dynamic>(sql, null).ToList();
                result.ForEach(item =>
                {
                    String[] hierarchy_describes = item.hierarchy_describe.Split('-');
                    item.hihierarchy_describe = hierarchy_describes[hierarchy_describes.Length - 1];
                });
            }catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }return result;

        }

        //获取整车数据
        [HttpPost,Route("getVehicleData"),AllowAnonymous]
        //public List<dynamic> GetVehicleData(string[] queryItems)
        public List<VehicleData> GetVehicleData(string vehicle_model,string announcement_model,string fuel_type,string pilothouse,string cab_suspension,string drive,string wheelbase,string frame)
        {
            List<VehicleData> result = new List<VehicleData>();
            //return GetMaterialNum(queryItems[0], queryItems[1], queryItems[2], queryItems[3], queryItems[4], queryItems[5], queryItems[6], queryItems[7]);  
            List<dynamic> materialnums = GetMaterialNum(vehicle_model, announcement_model, fuel_type, pilothouse, cab_suspension, drive, wheelbase, frame);
            int id = 1;
            materialnums.ForEach(item =>
            {
                VehicleData vehicleData = new VehicleData();
                vehicleData.value = item.materialnum;
                vehicleData.vehicleName = item.materialnum_name;
                List<dynamic> data_temp=GetTableRow(item.materialnum);
                List<Data> datas=new List<Data>();
                data_temp.ForEach(x =>
                {
                    Data data = new Data();
                    data.id = (id++).ToString();
                    data.module = x.hihierarchy_describe;
                    data.description = x.describe;
                    data.materialnum = x.componentnum;
                    data.note = "";
                    data.vehicle_materialnum=item.materialnum;
                    data.tree_id = 0;
                    if (x.count > 0) data.hasChildren = true;
                    datas.Add(data);
                });
                vehicleData.data = datas;
                result.Add(vehicleData);
            });
            return result;
        }

        //获取子节点数据
        [HttpPost, Route("getChildrenDatas"), AllowAnonymous]
        public List<Data> GetChildrenDatas(string parent,string materialnum,int tree_id)
        {
            List<Data> result = new List<Data>();
            try
            {
                string sql = "select materialnum,componentnum,description,(select count(*) from yjy_whole_wlzb yww2 where yww2.parent_componentnum=yww.componentnum and yww2.tree_id="+(tree_id+2)+") as count from yjy_whole_wlzb yww where parent_componentnum ='" + parent + "' and materialnum='"+materialnum+"' and tree_id="+(tree_id+1)+" group by componentnum,description,materialnum order by componentnum";
                List<dynamic> data_original= DBServerProvider.SqlDapper.QueryList<dynamic>(sql, null).ToList();
                data_original.ForEach(item =>
                {
                    Data data = new Data();
                    data.id = Guid.NewGuid().ToString();
                    data.module = item.componentnum;
                    data.description = item.description;
                    data.materialnum = item.componentnum;
                    data.note = "";
                    data.vehicle_materialnum=item.materialnum;
                    data.tree_id = tree_id+1;
                    if (item.count > 0)
                    {
                        data.hasChildren = true;
                    }
                    result.Add(data);
                });
            }catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return result;
        }
    }

    public class VehicleData
    {
        public string value;
        public string vehicleName;
        public List<Data> data;
    }
    public class Data
    {
        public string id;
        public string module;
        public string description;
        public string materialnum;
        public string note;
        public string vehicle_materialnum;
        public int tree_id;
        public bool hasChildren;

    }
}
